var async = require('async');
var moment = require('moment');

var connPool = require('./connPool.js');
var LoginBean = require('../jsBean/LoginBean');
var Result = require('../jsBean/Result');

module.exports = {
	index: function (req, res, loginbean) {
		
		// 转换时间戳格式
		function timeTransform (time) {
			return moment(time).format('YYYY-MM-DD HH:mm:ss');
		}
		
		
		
		var pool = connPool();
	
		pool.getConnection(function (err, conn) {
			if(err) {
				console.log(err.message);
				
				return;
			}

			
			async.series({
				selectRoll : function(done) {
					var selectRollSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 3;';
					var param = ["滚动"];
					conn.query(selectRollSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectHot : function(done) {
					var selectHotSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account ORDER BY n.sendTime DESC LIMIT 4;';
					var param = [];
					conn.query(selectHotSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectSociety : function(done) {
					var selectSocietySql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 4;';
					var param = ["社会"];
					conn.query(selectSocietySql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectAmusement : function(done) {
					var selectAmusementSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 4;';
					var param = ["娱乐"];
					conn.query(selectAmusementSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectScience : function(done) {
					var selectScienceSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 4;';
					var param = ["科技"];
					conn.query(selectScienceSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectPE : function(done) {
					var selectPESql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 4;';
					var param = ["体育"];
					conn.query(selectPESql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				},
				
				selectFinance : function(done) {
					var selectFinanceSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account WHERE n.classify=? ORDER BY n.sendTime DESC LIMIT 4;';
					var param = ["财经"];
					conn.query(selectFinanceSql, param, function (err, rs) {
						if(err) {
							console.log(err.message);
							
							return;
						}
						
						done(null, rs);
					});
				}
	
			}, function(err, rs) {
				if(err) {
					conn.release();
					console.log(err);
					
					return;
				}
				
				conn.release();
				
				var rollNews = rs['selectRoll'];
				var hotNews = rs['selectHot'];
				hotNews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				var societyNews = rs['selectSociety'];
				societyNews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				var amusementNews = rs['selectAmusement'];
				amusementNews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				var scienceNews = rs['selectScience'];
				scienceNews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				var PENews = rs['selectPE'];
				PENews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				var financeNews = rs['selectFinance'];
				financeNews.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				
				
				res.render('index', { 
					loginbean: loginbean,
					rollNews: rollNews,
					hotNews: hotNews,
					societyNews: societyNews,
					amusementNews: amusementNews,
					scienceNews: scienceNews,
					PENews: PENews,
					financeNews: financeNews
				});
			});
		});
	},
	
	nextPage: function (req, res, loginbean) {
		// 转换时间戳格式
		function timeTransform (time) {
			return moment(time).format('YYYY-MM-DD HH:mm:ss');
		}
		
		var page = parseInt(req.body['page']);
		var pool = connPool();
	
		pool.getConnection(function (err, conn) {
			if(err) {
				console.log(err.message);
				
				return;
			}
			
			var selectNewsSql = 'SELECT n.*, u.name FROM news AS n JOIN user AS u ON n.sendCount=u.account ORDER BY n.sendTime DESC LIMIT ?,4;';
			var param = [page];
			conn.query(selectNewsSql, param, function (err, rs) {
				if(err) {
					console.log(err.message);
					
					return;
				}
				
				conn.release();
				
				rs.forEach(function (item) {
					item.sendTime = timeTransform(item.sendTime);
				});
				
				var result = new Result();
				result.status = 1;
				result.data = rs;
				
				res.send(result);
			});
		});
	}
};